// 平滑滚动效果
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
        e.preventDefault();
        const target = document.querySelector(this.getAttribute('href'));
        if (target) {
            target.scrollIntoView({
                behavior: 'smooth'
            });
        }
    });
});

// 移动端导航栏交互
document.addEventListener('DOMContentLoaded', function() {
    const menuToggle = document.getElementById('menuToggle');
    const navLinks = document.querySelector('.nav-links');

    menuToggle.addEventListener('click', function() {
        navLinks.classList.toggle('active');
        // 切换图标
        const icon = menuToggle.querySelector('i');
        icon.classList.toggle('fa-bars');
        icon.classList.toggle('fa-times');
    });

    // 点击导航链接后关闭菜单
    document.querySelectorAll('.nav-links a').forEach(link => {
        link.addEventListener('click', () => {
            navLinks.classList.remove('active');
            const icon = menuToggle.querySelector('i');
            icon.classList.add('fa-bars');
            icon.classList.remove('fa-times');
        });
    });

    // 点击页面其他区域关闭菜单
    document.addEventListener('click', (e) => {
        if (!e.target.closest('.navbar')) {
            navLinks.classList.remove('active');
            const icon = menuToggle.querySelector('i');
            icon.classList.add('fa-bars');
            icon.classList.remove('fa-times');
        }
    });
});

// Banner 轮播图功能
const bannerSlider = document.querySelector('.banner-slider');
const slides = document.querySelectorAll('.banner-slide');
const prevBtn = document.querySelector('.banner-btn.prev');
const nextBtn = document.querySelector('.banner-btn.next');
const dotsContainer = document.querySelector('.banner-dots');

let currentSlide = 0;
const slideCount = slides.length;

// 创建轮播点
slides.forEach((_, index) => {
    const dot = document.createElement('div');
    dot.classList.add('banner-dot');
    if (index === 0) dot.classList.add('active');
    dot.addEventListener('click', () => goToSlide(index));
    dotsContainer.appendChild(dot);
});

// 更新轮播点状态
function updateDots() {
    document.querySelectorAll('.banner-dot').forEach((dot, index) => {
        dot.classList.toggle('active', index === currentSlide);
    });
}

// 切换到指定幻灯片
function goToSlide(index) {
    currentSlide = index;
    bannerSlider.style.transform = `translateX(-${currentSlide * 33.333}%)`;
    updateDots();
}

// 下一张幻灯片
function nextSlide() {
    currentSlide = (currentSlide + 1) % slideCount;
    goToSlide(currentSlide);
}

// 上一张幻灯片
function prevSlide() {
    currentSlide = (currentSlide - 1 + slideCount) % slideCount;
    goToSlide(currentSlide);
}

// 添加按钮事件监听
prevBtn.addEventListener('click', prevSlide);
nextBtn.addEventListener('click', nextSlide);

// 自动轮播
let autoSlideInterval = setInterval(nextSlide, 5000);

// 鼠标悬停时暂停自动轮播
bannerSlider.addEventListener('mouseenter', () => {
    clearInterval(autoSlideInterval);
});

// 鼠标离开时恢复自动轮播
bannerSlider.addEventListener('mouseleave', () => {
    autoSlideInterval = setInterval(nextSlide, 5000);
});

// 平滑滚动到指定区域
function scrollToSection(sectionId) {
    const section = document.getElementById(sectionId);
    if (section) {
        section.scrollIntoView({ 
            behavior: 'smooth',
            block: 'start'
        });
    }
} 